package cn.dnui_yxk801.dnuifood_16110100801.controller;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_yxk801.dnuifood_16110100801.MainActivity;
import cn.dnui_yxk801.dnuifood_16110100801.R;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.model.UserModel;

import static cn.dnui_yxk801.dnuifood_16110100801.MainActivity.userid;

public class ModifyCommentsActivity extends BaseActivity implements OnRetrofitListener,View.OnClickListener{
    private EditText modify_et;
    private Button modify_btn;
    private Button insert_btn;
    private String item_id;
    private String content;
    private int flag;

    @Override
    void initViews() {
        layout_file=R.layout.activity_modify_comments;
        setLayout(layout_file);
        modify_et=findViewById(R.id.modify_et_comment);
        modify_btn=findViewById(R.id.modify_btn);
        insert_btn=findViewById(R.id.insert_btn);
        Intent intent=getIntent();
        item_id=intent.getStringExtra("item_id");
        //if (intent.getStringExtra("content")!=null)
        content=intent.getStringExtra("content");
        flag=intent.getIntExtra("flag",3);
        //content=intent.getStringExtra("content");
        modify_et.setText(content);

        if (flag==1)
            modify_btn.setVisibility(View.INVISIBLE);
        if (flag==0)
            insert_btn.setVisibility(View.INVISIBLE);
    }

    @Override
    void initEvents() {
        modify_btn.setOnClickListener(this);
        insert_btn.setOnClickListener(this);
    }
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.insert_btn:
                content=modify_et.getText().toString();
                UserModel userModel=new UserModel();
                userModel.insertComment(item_id,content,this);
                break;
            case R.id.modify_btn:
                content=modify_et.getText().toString();
                UserModel userModel1=new UserModel();
                userModel1.updateComment(item_id,content,this);
                break;
    }}
    @Override
    void initDatas() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onSuccess(Object object, int flag) {
        if (flag==11){
            ResultBean resultBean=(ResultBean)object;
            if (resultBean.getSuccess().equals("1"))
                Toast.makeText(ModifyCommentsActivity.this,"评论成功",Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(ModifyCommentsActivity.this,"评论失败",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(ModifyCommentsActivity.this, MainActivity.class);
            intent.putExtra("userid",userid);
            startActivity(intent);
        }
        if (flag==3){
            ResultBean resultBean=(ResultBean)object;
            if (resultBean.getSuccess().equals("1"))
                Toast.makeText(ModifyCommentsActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(ModifyCommentsActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(ModifyCommentsActivity.this, MainActivity.class);
            intent.putExtra("userid",userid);
            startActivity(intent);
        }
    }

    @Override
    public void onFalure(String msg) {

    }
}
